package com.serkol.ams.controller.api;


import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.Navigation;
import com.serkol.ams.entity.Notice;
import com.serkol.ams.service.NoticeService;
import com.serkol.ams.service.NotifyService;
import com.serkol.annotation.IsLogin;
import com.serkol.constant.AuthConstant;
import com.serkol.constant.ResultCode;
import com.serkol.exception.AuthException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/notice")
public class NoticeApiController {


    @Autowired
    private NoticeService service;

    @Value("${upload.host}")
    private String uploadhost;

    @GetMapping("/findByList")
    @ResponseBody
    public Result findByList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "current", defaultValue = "0") int current) {
        AuthConstant.setPathUrl(uploadhost);
        return service.findByPageList(pageSize, current);

    }
    @GetMapping("/list")
    public Result list() {
        List<Map<String, Object>> list = service.findByList();
        return Result.ok(list);

    };

    //myView
    @GetMapping("/myView")
    public Result myView(@RequestParam(name = "ids", defaultValue = "0") String ids) {
        String[] list={};
        if(StringUtils.isNotEmpty(ids) && !ids.equals("0")){
            list=ids.split(",");
        }
        System.out.println("LLLLLLLLLLLLLLLLL:"+list.length);
        Map<String, Object> data = service.findByView(list);
        return data!=null?Result.ok(data):Result.failure();

    };
    @PostMapping("/edit")
    public Result edit(@RequestBody Notice data) {
        boolean flag = false;
        if (data.getId() != null && data.getId() > 0) {
            flag = service.updateById(data);
        } else {
            flag = service.save(data);
        }
        if (flag) {
            return Result.ok();
        } else {
            return Result.failure();
        }

    }


    @IsLogin
    @PostMapping("/byDelete")
    @ResponseBody
    public Result byDelete(@RequestParam(value = "id", defaultValue = "0") long id) throws AuthException {
        if(id>0) {
            boolean b = service.byDelete(id);
            if (b) {
                return Result.ok();
            } else {
                throw new AuthException(ResultCode.REQUEST_ERROR_NOT_ACCEPTABLE);
            }
        }else{
            throw new AuthException(ResultCode.REQUEST_ERROR_NOT_FOUND);
        }
    }


}
